'''
业务层：实现功能逻辑（注册和登录）
'''
import orm

# 注册
def register():
    while True:
        user = input("请输入想要注册的用户名：")
        pwd1 = input("请输入密码：")
        pwd2 = input("请再次输入密码：")
        if pwd1 == pwd2:
            sql = f""" 
            insert into users values(null,'{user}','{pwd2}')
            """
            print('注册成功')
            rows = orm.write_data(sql)
            if rows > 0:
                tag = 1
            return tag
            # break
        else:
            print("两次密码不一致，请重新输入")
            continue

def login():
    count = 0
    while True:
        tag = 0
        user = input('请输入用户名：')
        pwd = input('请输入密码：')
        sql = f"""
            select * from users where username = {user} and password = {pwd}
        """
        data = orm.read_data(sql)
        if data != ():
            print('登陆成功')
            tag = 1
        if tag == 0:
            count += 1
            if count == 3:
                print("用户已被锁定")
                break
            else:
                print(f"用户名或密码错误，请重新输入！还剩{3 - count}次登录机会")
                continue
        else:
            break
    return tag


if __name__ == '__main__':
    # # 测试一下 assert()
    print(login())
    # 测试一下 register()
    # print(register())
